package com.everwin.web.controller.carInfo;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.everwin.common.core.controller.BaseController;
import com.everwin.common.core.domain.AjaxResult;
import com.everwin.common.core.page.TableDataInfo;
import com.everwin.common.json.JsonUtil;
import com.everwin.web.domain.CarInfo;
import com.everwin.web.service.CarInfoService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/car")
@CrossOrigin
public class CarInfoController extends BaseController {

    @Autowired
    CarInfoService carInfoService;

    /**
     * 查询 车辆抵押信息
     */
    @RequestMapping("/info/data/list")
    public TableDataInfo queryCreditStrategyList(CarInfo request) {
        startPage();
        List<CarInfo> list = carInfoService.queryCarInfoList(request);
        logger.info("车辆抵押信息查询结果:{}", JsonUtil.toJson(list));
        return getDataTable(list);
    }

    /**
     * 上传 excel 导入数据
     */
    @SneakyThrows
    @RequestMapping("/info/data/upload")
    public AjaxResult uploadFile(MultipartFile file, HttpServletRequest request) {

        String originalFilename = file.getOriginalFilename();
        logger.info("导入的文件名称:{},是否更新已存在数据:{}", originalFilename, request.getParameter("cover"));

        ImportParams params = new ImportParams();
        // 表格标题行数 默认0    此处无标题
        params.setTitleRows(0);
        // 表头行数,默认1
        params.setHeadRows(1);

        List<CarInfo> carInfoList = ExcelImportUtil.importExcel(file.getInputStream(), CarInfo.class, params);
        logger.info("导入数据明细:{}", JsonUtil.toJson(carInfoList));

        return AjaxResult.success();
    }
}
